<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            margin: 0;
        }

        .login-container {
            background-color: white;
            padding: 20px;
            border-radius: 5px;
            box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
            width: 300px;
            margin: 0 auto;
        }

        h2 {
            text-align: center;
        }

        .form-group {
            margin-bottom: 15px;
        }

        label {
            display: block;
            margin-bottom: 5px;
        }

        input {
            width: 100%;
            padding: 10px;
            border: 1px solid #ccc;
            border-radius: 3px;
        }

        button {
            width: 100%;
            padding: 10px;
            background-color: #007bff;
            color: white;
            border: none;
            border-radius: 3px;
            cursor: pointer;
        }

        button:hover {
            background-color: #0056b3;
        }
        .one img{
            width: 100%;
            height: 100px;
        }
    </style>

</head>
<body>
<div class="one">
    <img src="./img/systemImages/bg-land.gif">

    <div class="login-container">
        <h2>登录页面</h2>
        <form id="loginForm">
            <div class="form-group">
                <label for="username">账号:</label>
                <input type="text" id="username" placeholder="请输入账号" required>
            </div>
            <div class="form-group">
                <label for="password">密码:</label>
                <input type="password" id="password" placeholder="请输入密码" required>
            </div>
            <button type="submit">登录</button>
        </form>
    </div>
</div>
<script>
    const loginForm = document.getElementById('loginForm');

    loginForm.addEventListener('submit', function (e) {
        e.preventDefault();
        const username = document.getElementById('username').value;
        const password = document.getElementById('password').value;
        if (username === '' || password === '') {
            alert('Please enter both username and password');
        } else {
            // 使用 AJAX 发送登录请求
            const xhr = new XMLHttpRequest();
            xhr.open('POST', 'LoginServlet', true);
            xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            xhr.onreadystatechange = function() {
                if (xhr.readyState === 4) {
                    if (xhr.status === 200) {
                        // 登录成功，跳转到 index.html
                        window.location.href = 'index.html';
                    } else {

                        window.location.href = 'index.html';
                    }
                }
            };
            // 发送登录信息
            xhr.send('username=' + encodeURIComponent(username) + '&password=' + encodeURIComponent(password));
        }
    });
</script>
</body>
</html>